package com.lxq.LeetCode.peakIndexInMountainArray;

public class Solution {
    public static void main(String[] args) {
        int[] arr = {3, 5, 3, 2, 0};
        System.out.println(peakIndexInMountainArray(arr));
    }

    public static int peakIndexInMountainArray(int[] arr) {
        int res = -1;
        res = findPeak(arr, 0, arr.length - 1);
        return res;
    }

    public static int findPeak(int[] arr, int l, int r) {
        int mid = l + (r - l) / 2;
        if (mid == 0 || mid == arr.length - 1) {
            if (mid == 0 && arr[mid] < arr[mid + 1]) {
                return mid + 1;
            } else if (mid == arr.length - 1 && arr[mid] < arr[mid - 1]) {
                return mid - 1;
            }
            return mid;
        }
        if (arr[mid - 1] < arr[mid] && arr[mid] > arr[mid + 1]) {
            return mid;
        }
        if (arr[mid] < arr[mid + 1]) {
            return findPeak(arr, mid + 1, r);
        } else {
            return findPeak(arr, l, mid - 1);
        }
    }
}
